<template>
  <div>
    <BasicTable @register="registerTable" :search-info="searchInfo" :rowSelection="rowSelection">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <ChangeTypeDrawer @register="registerChangeTypeDrawer" @success="reload()" />
  </div>
</template>
<script lang="ts" setup name="fullDeductionDetail">
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { columns, searchFormSchema } from './data/data';
import { Api } from './data/api';
import { reactive } from 'vue';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import ChangeTypeDrawer from './components/ChangeTypeDrawer.vue';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';

const { createConfirm } = useMessage();
const { prefixCls, tableContext, setProps, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'fullDeductionDetail-template',
  tableProps: {
    //表格标题
    title: '全额抵扣详情列表',
    //主键字段
    rowKey: 'fullDeductionDetailId',
    //列表接口
    listUrl: Api.page,
    //列表查询handler
    handlerName: 'busFullDeductionDetailListHandler',
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      showExportButton: true,
    },
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;
const [registerChangeTypeDrawer, { openDrawer: openChangeTypeDrawer }] = useDrawer();
const [registerChangeMallTypeDrawer, { openDrawer: openChangeMallTypeDrawer }] = useDrawer();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '抵扣撤销(商城)',
      onClick: handlerSetRevokeState.bind(null, record),
      ifShow: record.fullDeductionDetailState == 0 && record.isMallFullDeduction,
      auth: 'ACTION_deductionDetailMallSetRevoke',
    },
    {
      label: '抵扣撤销(非商城)',
      onClick: handlerSetRevokeState.bind(null, record),
      ifShow: record.fullDeductionDetailState == 0 && !record.isMallFullDeduction,
      auth: 'ACTION_deductionDetailOtherSetRevoke',
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '转抵扣类型',
      onClick: handlerChangeType.bind(null, record),
      ifShow: record.fullDeductionDetailState == 0 && !record.isMallFullDeduction,
      auth: 'ACTION_deductionDetailChangeType',
    },
  ];
}

/**
 * 抵扣日志撤销
 */
function handlerSetRevokeState(record) {
  createConfirm({
    iconType: 'info',
    title: '确认抵扣日志撤销吗？',
    content: '请确认会员是否存在抵扣记录，谨慎操作，抵扣日志撤销会将本条抵扣记录和抵扣日志标记为撤销状态？',
    onOk: async () => {
      await handlePost(Api.setRevokeState, { fullDeductionDetailId: record.fullDeductionDetailId });
      reload();
    },
  });
}

/**
 * 转抵扣类型
 */
function handlerChangeType(record) {
  openChangeTypeDrawer(true, {
    record,
    isUpdate: true,
  });
}

const searchInfo = reactive<Recordable>({});

function setSearchInfoProps(fullDeductionId) {
  console.info('fullDeductionId', fullDeductionId);
  searchInfo.fullDeductionId = fullDeductionId;
}

defineExpose({
  setSearchInfoProps,
});
</script>
